<template>
  <div>
    <el-form
      ref="editFormRef"
      :rules="rules"
      :model="editForm.data"
      label-width="120"
    >
      <el-form-item label="设备厂家" prop="vendor">
        <el-select
          v-model="editForm.data.vendor"
          filterable
          style="width: 320px"
          placeholder="请选择设备厂家"
        >
          <el-option
            v-for="item in vendor.data"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="设备编号" prop="code">
        <el-input
          disabled
          v-model="editForm.data.code"
          maxlength="20"
          style="width: 320px"
          placeholder="请输入设备编号"
        />
      </el-form-item>

      <el-form-item label="使用状态" prop="state">
        <el-select
          v-model="editForm.data.state"
          filterable
          style="width: 320px"
          placeholder="请选择使用状态"
        >
          <el-option
            v-for="item in state.data"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <div class="footer">
          <div></div>
          <div>
            <el-button class="reset-btn" @click="resetForm(editFormRef)"
              >取 消</el-button
            >
            <el-button class="confirm-btn" @click="submitForm(editFormRef)"
              >确 认</el-button
            >
          </div>
        </div>
      </el-form-item>
    </el-form>
  </div>
</template>

<script setup>
import { reactive, ref } from "vue";
import {
  getEquipmentSupplier,
  getDictionary,
} from "@/service/modules/publicModule";
import { getEditGeomagnetism } from "../request.js";

const props = defineProps({
  operationId: String,
});

const vendor = reactive({
  data: [],
});

const state = reactive({
  data: [],
});

const editFormRef = ref("");

const editForm = reactive({
  data: {
    id: props.operationId.id,
    code: props.operationId.code,
    vendor: props.operationId.vendorId,
    state: props.operationId.state,
  },
});

const rules = reactive({
  vendor: [{ required: true, message: "请选择设备厂家", trigger: "change" }],
  code: [{ required: true, message: "请输入设备编号", trigger: "blur" }],
  state: [{ required: true, message: "请选择使用状态", trigger: "change" }],
});

const getData = async () => {
  await getEquipmentSupplier("1").then((res) => {
    vendor.data = res.data.result;
  });
  await getDictionary("device_use_state").then((res) => {
    state.data = res.data.result;
  });
};

getData();

const submitForm = (formEl) => {
  if (!formEl) return;
  formEl.validate((valid) => {
    if (valid) {
      getEditGeomagnetism(editForm.data).then((res) => {
        if (res.data.code === 0) {
          ElMessage({
            type: "success",
            message: `地磁[${editForm.data.code}]修改信息成功`,
          });
          emits("operationDone");
        } else {
          ElMessage.error(res.data.msg);
        }
      });
    }
  });
};

const resetForm = (formEl) => {
  if (!formEl) return;
  editForm.data = {};
  formEl.resetFields();
  emits("operationDone");
};
const emits = defineEmits(["operationDone"]);
</script>

<style lang="less" scoped>
.footer {
  width: 100%;
  display: flex;
  justify-content: space-between;
}
.reset-btn {
  width: 65px;
  height: 32px;
  background: #ffffff;
  border-radius: 2px;
  border: 1px solid #d9d9d9;
  font-weight: 400;
  font-size: 14px;
  color: rgba(0, 0, 0, 0.65);
}
.confirm-btn {
  width: 65px;
  height: 32px;
  background: #007aff;
  border-radius: 2px;
  font-weight: 400;
  font-size: 14px;
  color: #ffffff;
}
</style>